/*
 * This file is part of the Refran Library
 * Copyright (C) 2009 César Enrique García
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * $Author:  $
 * $Date:  $
 * $Revision: $
 * $Name:  $
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifndef REFRAN_RECIPE_MANAGER_LOCAL_SHLIB_H
#define REFRAN_RECIPE_MANAGER_LOCAL_SHLIB_H

#include <vector>
#include <map>
#include "Recipe.h"

namespace Refran
{
    /**
     * This class eases the search of shared libraries that contain recipes
     * in a given path. It encapsulates the loading and unloading of the shared
     * libraries and the registering with the recipe factory.
     * Typical use to have all the recipes available to instantiate:
     * @code
     *  RecipeManagerLocalShlib manager("/path");
     *  manager.registerAllRecipes();
     *  auto recipeNames = RecipeFactory::instance().getRecipeNameList();
     *
     * Typical use if we are interested in only one recipe:
     * @code
     *  RecipeManagerLocalShlib manager("/path");
     *  auto recipeNames = manager.getAvailableRecipeNames();
     *  string myRecipe = recipeNames[0];
     *  manager.registerRecipe(myRecipe);
     */
    class RecipeManagerLocalShlib
    {
        public:

        /**
         * Default constructor
         */
        RecipeManagerLocalShlib();

        /**
         * Constructor from a single path
         */
        RecipeManagerLocalShlib(const std::string& path);

        /**
         * Constructor from a list of paths
         */
        RecipeManagerLocalShlib(const std::vector<std::string>& paths);

        /**
         * Destructor
         */
        virtual ~RecipeManagerLocalShlib();

        /**
         * Adds a path where to look for recipes.
         * Subdirectories will also be added
         */
        void addShlibPath(const std::string& path);

        /**
         * Clean the shared libraries search path
         * @return
         */
        void clearShlibPaths();

        /**
         * Sets all the paths where to look for recipes.
         * Subdirectories will also be added
         */
        void setShlibPaths(const std::vector<std::string>& paths);

        /**
         * Search for all the recipes available in the shared libraries paths
         * and subdirs
         */
        std::vector<std::string> getAvailableRecipeNames();

        /**
         * Load a recipe and register it in the RecipeFactory
         */
        void registerRecipe(const std::string& recipeName);

        /**
         * Load all recipes found in the path of shared libraries
         * and register them in the RecipeFactory
         */
        void registerAllRecipes();

        private:

        /**
         * Search in all the subdirectories for recipe plugins
         */
        void scanPathForRecipes_();

        /**
         * The searching path
         */
        std::vector<std::string> m_shlibPaths;

        /**
         * The list of available recipes within the path
         */
        std::map<std::string, std::map<unsigned int, std::string> >
            m_recipeNameVersionLibpath;

        /**
         * Whether the paths have been searched for recipes
         */
        std::vector<bool> m_pathHasBeenSearched;

        /**
         * Mutex for the path to the recipes
         */
        std::mutex m_pathMutex;

    };


}// namespace Refran

#endif // REFRAN_RECIPE_MANAGER_LOCAL_SHLIB_H
